package com.franzliszt.OrderOnline.Activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.franzliszt.OrderOnline.SQLite.Dao;
import com.franzliszt.OrderOnline.adapter.contentAdapter;
import com.franzliszt.OrderOnline.adapter.titleAdapter;
import com.franzliszt.OrderOnline.sunzhengyi.LoginPackage.Login;
import com.franzliszt.OrderOnline.tool.Goods;
import com.franzliszt.OrderOnline.tool.GoodsList;
import com.franzliszt.OrderOnline.tool.resources;
import com.franzliszt.OrderOnline.util.KillProcess;
import com.franzliszt.OrderOnline.util.SP;
import com.franzliszt.hospitalnumberingsystem.R;


import androidx.recyclerview.widget.LinearLayoutManager;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;



public class FunctionActivity extends AppCompatActivity {
    private RecyclerView mGoodsTitle,mGoodsContent;
    private com.franzliszt.OrderOnline.adapter.contentAdapter contentAdapter;
    private com.franzliszt.OrderOnline.adapter.titleAdapter titleAdapter;
    private List<Goods.GoodsTitle> titleList = new ArrayList<>(  );
    private List<Goods.GoodsContent> contentList = new ArrayList<>(  );
    private Goods.GoodsTitle goodsTitle;
    private Goods.GoodsContent goodsContent;
    private LinearLayoutManager titleManager,contentManager;
    private ImageView manIcon;
    private TextView money,other;
    private TextView settlement;
    private double TotalMoney;
    private Dao dao;
    private GoodsList goodsList;
    private Context context = null;
    private KillProcess killProcess;
    private SP sp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        if (Build.VERSION.SDK_INT >= 21) {
            View decorView = getWindow().getDecorView();
            decorView.setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE );
            getWindow().setStatusBarColor( Color.TRANSPARENT );
        }
        setContentView( R.layout.activity_function );
        InitView();
        InitRecyclerView();
        InitTitle();
        InitContent();
        InitListener();
        settlement.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dao.Delete();
                GotoSettlement();
                startActivity( new Intent( context,GoodsListActivity.class ) );
            }
        } );
    }
    private void InitView(){
        mGoodsTitle = findViewById( R.id.titleRecyclerView );
        mGoodsContent = findViewById( R.id.contentRecyclerView );
        manIcon = findViewById( R.id.qishouIcon );
        money = findViewById( R.id.money );
        other = findViewById(R.id.exchangeFunc);
        other.setVisibility(View.GONE);
        settlement = findViewById( R.id.settlement );
        if (context == null){
            context = FunctionActivity.this;
        }
        dao = new Dao( context );
        killProcess = KillProcess.getInstance();
        killProcess.addActivity( FunctionActivity.this );
        sp = SP.getInstance();
    }
    private void InitRecyclerView(){
        titleManager = new LinearLayoutManager( getApplicationContext() );
        mGoodsTitle.setLayoutManager( titleManager );
        titleAdapter = new titleAdapter( R.layout.title_item,titleList );
        mGoodsTitle.setAdapter( titleAdapter );



        contentManager = new LinearLayoutManager( getApplicationContext() );
        mGoodsContent.setLayoutManager( contentManager );
        contentAdapter = new contentAdapter( R.layout.content_item,contentList );
        mGoodsContent.setAdapter( contentAdapter );

    }
    private void InitTitle(){
        String[] titleArray = getResources().getStringArray(R.array.title);
        for (int i = 0; i < titleArray.length; i++) {
            goodsTitle = new Goods.GoodsTitle(titleArray[i],i );
            titleList.add( goodsTitle );
        }
    }
    private void InitContent(){
        TypedArray iconArray = getResources().obtainTypedArray(R.array.icon);
        int index = 0;
        String[] content = getResources().getStringArray(R.array.content);
        for (int i = 0; i < content.length ; i+=3) {
            //if (index < iconArray.length())
            /*把酒言欢*/
            if ( i < 3){
                goodsContent = new Goods.GoodsContent(  iconArray.getResourceId(index++,0), content[i], content[i+1], Float.parseFloat(content[i+2]),R.drawable.icon_add,R.drawable.icon_sub,0,0 );
            }else if (i < 8){
                /*爆炒干锅*/
                goodsContent = new Goods.GoodsContent( iconArray.getResourceId(index++,0), content[i], content[i+1], Float.parseFloat(content[i+2]),R.drawable.icon_add,R.drawable.icon_sub,0,1 );
            }else if ( i  < 12){
                /*海鲜派对*/
                goodsContent = new Goods.GoodsContent( iconArray.getResourceId(index++,0), content[i], content[i+1], Float.parseFloat(content[i+2]),R.drawable.icon_add,R.drawable.icon_sub,0,2 );
            }else if ( i < 16){
                /*辣度自选*/
                goodsContent = new Goods.GoodsContent( iconArray.getResourceId(index++,0), content[i], content[i+1], Float.parseFloat(content[i+2]),R.drawable.icon_add,R.drawable.icon_sub,0,3 );
            }else if ( i < 22){
                /*素菜最爱*/
                goodsContent = new Goods.GoodsContent( iconArray.getResourceId(index++,0), content[i], content[i+1], Float.parseFloat(content[i+2]),R.drawable.icon_add,R.drawable.icon_sub,0,4 );
            }else if ( i < 25){
                /*旺季龙虾*/
                goodsContent = new Goods.GoodsContent( iconArray.getResourceId(index++,0), content[i], content[i+1], Float.parseFloat(content[i+2]),R.drawable.icon_add,R.drawable.icon_sub,0,5 );
            }else if ( i < 28){
                /*无肉不欢*/
                goodsContent = new Goods.GoodsContent( iconArray.getResourceId(index++,0), content[i], content[i+1], Float.parseFloat(content[i+2]),R.drawable.icon_add,R.drawable.icon_sub,0,6 );
            }else
                /*主食饱腹*/
                goodsContent = new Goods.GoodsContent( iconArray.getResourceId(index++,0), content[i], content[i+1], Float.parseFloat(content[i+2]),R.drawable.icon_add,R.drawable.icon_sub,0,7 );

            contentList.add( goodsContent );
        }
    }
    private void InitListener(){
        titleAdapter.setOnItemClickListener( new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                String[] titleArray = getResources().getStringArray(R.array.title);
                titleAdapter.setSelection( position );
                titleAdapter.notifyDataSetChanged();
                contentManager.scrollToPositionWithOffset( titleArray.length,0 );
            }
        } );
        mGoodsContent.addOnScrollListener( new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled( recyclerView, dx, dy );
                int firstPosition = contentManager.findFirstVisibleItemPosition();
                goodsContent = contentList.get(firstPosition);
                int subId = goodsContent.getSubId();
                int pos = 0;
                for (int i = 0; i < titleList.size(); i++) {
                    int id = titleList.get(i).getId();
                    if ((subId == id)) {
                        pos = i;
                    }
                }
                titleAdapter.setSelection(pos);
                //mGoodsTitle.scrollToPosition( pos );
                titleAdapter.notifyDataSetChanged();

            }
        } );
        contentAdapter.setOnItemChildClickListener( new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                double  Price = contentList.get( position ).getPrice();
                switch (view.getId()){
                    case R.id.Add:
                        int numberAdd = contentList.get( position ).getNumber();
                        numberAdd++;
                        Monetary(Price);
                        contentList.get( position ).setNumber( numberAdd );
                        contentAdapter.notifyDataSetChanged();
                        //Toast.makeText( FunctionActivity.this,"Add"+Price,Toast.LENGTH_SHORT ).show();
                        break;
                    case R.id.Sub:
                        int numberSub = contentList.get( position ).getNumber();
                        if (numberSub > 0 ){
                            numberSub--;
                            contentList.get( position ).setNumber( numberSub );
                            LessMoney(Price);
                        }else if (numberSub < 0){
                            contentList.get( position ).setNumber( 0 );
                        }
                        contentAdapter.notifyDataSetChanged();
                        //Toast.makeText( FunctionActivity.this,"Sub"+Price,Toast.LENGTH_SHORT ).show();
                        break;
                }
            }
        } );
    }
    private void GotoSettlement(){
        String user = (String) sp.GetData(FunctionActivity.this,"UserName_login","");
        String pass = (String) sp.GetData(FunctionActivity.this,"PassWord_login","");
        for (int i = 0; i <contentList.size() ; i++) {
            int num = contentList.get( i ).getNumber();
            if (num > 0){
                /*插入数据为null*/
                //Toast.makeText( context,""+num,Toast.LENGTH_SHORT ).show();
                goodsList = new GoodsList(user,pass, contentList.get( i ).getGoodsIMG(),contentList.get( i ).getContent(),contentList.get( i ).getPrice(),contentList.get( i ).getNumber(),R.drawable.icon_sub,R.drawable.icon_add );
                dao.Insert( goodsList );
            }
        }
    }
    /*样式*/
    private void MoreThanStyle(){
        manIcon.setImageDrawable( getResources().getDrawable( R.drawable.qishou_yellow ) );
        settlement.setBackground( getResources().getDrawable(R.drawable.btn_settlement) );
        settlement.setText( "去结算" );
        settlement.setEnabled( true );
    }
    private void LessStyle(){
        manIcon.setImageDrawable( getResources().getDrawable( R.drawable.qishou_grey ) );
        settlement.setBackground( getResources().getDrawable(R.drawable.btn_settlement_no) );
        settlement.setText( "15起送" );
        settlement.setEnabled( false );
    }
    /*计算金额*/
    private void Monetary(double Money){
        TotalMoney+=Money;
        money.setText( doubleToString(TotalMoney) );
        if (TotalMoney > 15){
            MoreThanStyle();
        }else {
            LessStyle();
        }
    }
    private void LessMoney(double Money){
        TotalMoney-=Money;
        if (TotalMoney < 15){
            LessStyle();
            money.setText( 0+"" );
        }else {
            MoreThanStyle();
            money.setText( doubleToString(TotalMoney) );
        }
    }
    public static String doubleToString(double num){
        return new DecimalFormat("0.00").format(num);
    }
    public void Exit(View view){
        startActivity( new Intent( context, Login.class ) );
    }
}
